package tests.steps;

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.CustomHtmlUnitDriver;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class NewPersonaSteps {

    private WebDriver driver;
    private WebElement webElement;

    @Given("^I have accessed HR system create page$")
    public void I_have_accessed_HR_system_create_page() {
        this.driver = new CustomHtmlUnitDriver();
        driver.get("http://localhost:8888/hr/manager/person/create");
    }

    @When("^I enter \"([^\"]*)\" in \"([^\"]*)\" field on create page$")
    public void I_enter_in_field_on_create_page(String value, String filed) {
        webElement = driver.findElement(By.name(filed));
        webElement.sendKeys(value);
    }

    @When("^click the \"([^\"]*)\" button$")
    public void click_the_button(String buttonName) {
        webElement = driver.findElement(By.name(buttonName));
        webElement.click();
    }

    @Then("^stuff result list contains \\(\"([^\"]*)\", \"([^\"]*)\", \"([^\"]*)\", \"([^\"]*)\"\\)$")
    public void stuff_result_list_contains_(String lastname, String firstname, String middlename, String jobtitle) {
        System.out.println(driver.getPageSource());
        webElement = driver.findElement(By.id("ln_1"));
        assertThat(webElement.getText(), is(lastname));
        webElement = driver.findElement(By.id("fn_1"));
        assertThat(webElement.getText(), is(firstname));
        webElement = driver.findElement(By.id("mn_1"));
        assertThat(webElement.getText(), is(middlename));
        webElement = driver.findElement(By.id("jt_1"));
        assertThat(webElement.getText(), is(jobtitle));
    }

    @Then("^link to create new person$")
    public void link_to_create_new_person() {
        webElement = driver.findElement(By.id("newPerson"));
        assertThat(webElement.getAttribute("href"), is("create"));
    }

}
